package com.foreveross.proxyip.remote;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.foreveross.crawl.common.cfg.PropertyUtils;
import com.foreveross.crawl.common.cfg.system.SystemPropertiesLoader;
import com.foreveross.proxyip.IProxyIpProvider;
import com.foreveross.proxyip.ProxyIpModel;
import com.foreveross.proxyip.ProxyipProperties;
import com.foreveross.proxyip.ProxyipProvideException;
import com.foreveross.proxyip.application.offer.IProxyIPService;
import com.foreveross.proxyip.application.offer.bean.ProxyIpDTO;
import com.foreveross.proxyip.local.ProxyIpSimplePool;

@Service("rmiProxyIpProvider")
public class RmiProxyIpProvider implements IProxyIpProvider {
	public Logger logger = LoggerFactory.getLogger(RmiProxyIpProvider.class);
	private static final String PROXYIP_SERVICE_AUTH = "proxyip.provider.service.auth";
	@Resource(name="useProxyIPService")
	protected IProxyIPService proxyIPRMIService;
	
	@Override
	public ProxyIpModel provideProxyIp() {
		ProxyIpModel ipModel = null;
		try {
			ProxyIpDTO dto=proxyIPRMIService.getProxyIp(getProxyipServiceAuth(), getLocalName());
			if(dto != null){
				ipModel = new ProxyIpModel(dto.getIp(), dto.getPort());
				ipModel.setLocation(ProxyIpModel.IP_LOCATION_RMI);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ProxyipProvideException(e);
		}
		return ipModel;
	}

	@Override
	public ProxyIpModel provideProxyIp(Serializable channelId) {
		ProxyIpModel ipModel = null;
		try {
			ProxyIpDTO dto = proxyIPRMIService.getChannelProxyIp(channelId.toString(), getProxyipServiceAuth(), getLocalName());
			if(dto != null){
				ipModel = new ProxyIpModel(dto.getIp(), dto.getPort());
				ipModel.setLocation(ProxyIpModel.IP_LOCATION_RMI);
				ipModel.setUserId(channelId);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new ProxyipProvideException(e);
		}
		return ipModel;
	}

	@Override
	public void rollBackProxyIp(ProxyIpModel model) {
		//2014-10-28 去除回滚动作
//		if(model == null || model.getHasRecycle()) return;
//		model.setHasRecycle(true);//标识已回收过了
//		ProxyIpDTO dto = new ProxyIpDTO(model.getIp(), model.getPort(), 
//				model.getUserId() != null ? model.getUserId().toString() : null);
//		if(model.getUseStatus() == ProxyIpModel.UNENBLE){
//			dto.changeUnable();
//		}
//		try {
//			proxyIPRMIService.recycle(dto, getProxyipServiceAuth());
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new ProxyipProvideException(e);
//		}
	}

	public String getProxyipServiceAuth(){
		return PropertyUtils.getProperty(PROXYIP_SERVICE_AUTH, ProxyipProperties.CONFIG_FILE);
	}
	
	public String getLocalName(){
		return PropertyUtils.getProperty("system.port", SystemPropertiesLoader.FILE_NAME);
	}
	public int getPoolSize(){
		return PropertyUtils.getIntProperty("proxyip.provider.simplepool.maxsize", ProxyipProperties.CONFIG_FILE, 
				ProxyIpSimplePool.PROXYIP_POOL_MAXSIZE);
	}
	@Override
	public List<ProxyIpModel> provideProxyIps() {
		List<ProxyIpModel> ips = new LinkedList<ProxyIpModel>();
		ProxyIpModel ipModel = null;
		List<ProxyIpDTO> dtos = null;
		try {
			dtos = proxyIPRMIService.getProxyIps(getPoolSize(), getProxyipServiceAuth(), getLocalName());
			if(dtos != null){
				for(ProxyIpDTO dto : dtos){
					ipModel = new ProxyIpModel(dto.getIp(), dto.getPort());
					ipModel.setLocation(ProxyIpModel.IP_LOCATION_RMI);
					ips.add(ipModel);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ProxyipProvideException(e);
		}finally{
			 ipModel = null;
			 dtos = null;
		}
		return ips;
	}

	@Override
	public List<ProxyIpModel> provideProxyIps(Serializable channelId) {
		List<ProxyIpModel> ips = new LinkedList<ProxyIpModel>();
		ProxyIpModel ipModel = null;
		List<ProxyIpDTO> dtos = null;
		try {
			dtos = proxyIPRMIService.getChannelProxyIps(channelId.toString(), getPoolSize(), getProxyipServiceAuth(), getLocalName());
			if(dtos != null){
				for(ProxyIpDTO dto : dtos){
					ipModel = new ProxyIpModel(dto.getIp(), dto.getPort());
					ipModel.setUserId(channelId);
					ipModel.setLocation(ProxyIpModel.IP_LOCATION_RMI);
					ips.add(ipModel);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ProxyipProvideException(e);
		}finally{
			 ipModel = null;
			 dtos = null;
		}
		return ips;
	}
	
}
